*Create Log
local file Compliers

capture log close
log using "$logs/`file'_$S_DATE.log", replace


 
 
use "$data_out/provider_propensity", clear


merge 1:1 pid_pde encdate using "$data_out/pretrends", keep(1 3) nogen
rename depression depression_pre
xtile pct_propensity = propensity, nq(200)

gen top_1 = pct_propensity >=199
gen bottom_1 = pct_propensity <= 2

cap postclose complier
postfile complier str16 Variable Constant Lenient Strict Always_Taker Never_Taker Population   using "$plotdir/complier_subgroup.dta", replace


reghdfe ed_prescription bottom_1 top_1 i.age_bin race_white female junior_enlisted college married longevity afqt_p, absorb(hym hd i.diag mos)  cluster(provID)

post complier ("Full Sample") (_b[_cons]) (_b[bottom_1]) (_b[top_1]) ((_b[bottom_1] + (_b[_cons]))) (1 -  (_b[_cons]) - (_b[top_1]))  (e(N)) 

preserve
keep if service == "A"

cap drop pct_propensity
cap drop top_1
cap drop bottom_1
xtile pct_propensity = propensity, nq(100)

gen top_1 = pct_propensity >=100
gen bottom_1 = pct_propensity <= 1



reghdfe ed_prescription bottom_1 top_1 i.age_bin race_white female junior_enlisted college married longevity afqt_p, absorb(hym hd i.diag mos)  cluster(provID) keepsingleton

post complier ("Army Sample") (_b[_cons]) (_b[bottom_1]) (_b[top_1]) ((_b[bottom_1] + (_b[_cons]))) (1 -  (_b[_cons]) - (_b[top_1]))  (e(N)) 

restore

foreach group in older younger non_white white male female single married hs college high_afqt low_afqt {
preserve

merge 1:1 pid_pde encdate using "$data_out/subgroup_`group'", keep (3) nogen

cap drop pct_propensity
cap drop top_1
cap drop bottom_1
xtile pct_propensity = propensity, nq(100)

gen top_1 = pct_propensity >=100
gen bottom_1 = pct_propensity <= 1

reghdfe ed_prescription bottom_1 top_1 i.age_bin race_white female junior_enlisted college married longevity afqt_p, absorb(hym hd i.diag mos)  cluster(provID) keepsingleton

post complier ("`group'") (_b[_cons]) (_b[bottom_1]) (_b[top_1]) ((_b[bottom_1] + (_b[_cons]))) (1 -  (_b[_cons]) - (_b[top_1]))  (e(N)) 
restore

}

rename substance_flag_pre substance_pre
rename adverse_flag_pre adverse_pre


foreach group of varlist depression_pre deploy_pre demotion_pre denied_pre revoke_pre promotion_pre any_flag_pre substance_pre crime_flag_pre adverse_pre {
forval i = 0/1 {
preserve


keep if `group' == `i'

cap drop pct_propensity
cap drop top_1
cap drop bottom_1
xtile pct_propensity = propensity, nq(100)

gen top_1 = pct_propensity >=100
gen bottom_1 = pct_propensity <= 1


reghdfe ed_prescription bottom_1 top_1 i.age_bin race_white female junior_enlisted college married longevity afqt_p, absorb(hym hd i.diag mos)  cluster(provID) keepsingleton

post complier ("`group'_`i'") (_b[_cons]) (_b[bottom_1]) (_b[top_1]) ((_b[bottom_1] + (_b[_cons]))) (1 -  (_b[_cons]) - (_b[top_1]))  (e(N)) 
restore

}
}

postclose complier





use "$plotdir/complier_subgroup.dta", clear

gen Population_Share = Population/1447758
gen Army_only = inlist(Variable,"adverse_flag_pre", "deploy_pre_0", "deploy_pre_1", "denied_pre_0", "denied_pre_1", "revoke_pre_0", "revoke_pre_1")
replace Army_only = 1 if inlist(Variable, "any_flag_pre_0", "any_flag_pre_1", "substance_pre_0", "substance_pre_1", "crime_flag_pre_0", "crime_flag_pre_1", "adverse_pre_0", "adverse_pre_1")



replace Population_Share = Population/719551 if Army_only ==1


gen Complier = 1 - Always_Taker - Never_Taker


gen rel_likelihood = Complier/0.3662188 if Army_only == 0
replace rel_likelihood = Complier/0.3858394 if Army_only == 1

gen Complier_Share = rel_likelihood * Population_Share
gen total = Complier_Share + Complier_Share[_n-1] if Army_only ==1 & substr(Variable,-1,1) =="1"



save  "$plotdir/complier_subgroup_export.dta", replace
